/*
 * @lc app=leetcode.cn id=572 lang=javascript
 *
 * [572] 另一个树的子树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} s
 * @param {TreeNode} t
 * @return {boolean}
 */
var isSubtree = function (s, t) {
    let isSameObject = function (a, b) {
        if (a == null && b == null) return true
        if (!(a && b)) return false
        if (a.val !== b.val) return false
        return isSameObject(a.left, b.left) && isSameObject(a.right, b.right)
    }
    // let flag = false
    // let dfs = function (node) {
    //     if (!node) return
    //     if (isSameObject(node,t)) {
    //         flag = true
    //     }
    //     dfs(node.left,t)
    //     dfs(node.right,t)
    // }
    // dfs(s)
    // return flag
    if(!s)return false
    if(isSameObject(s,t))return true
    return isSubtree(s.left,t)||isSubtree(s.right,t)
};
// @lc code=end

let isSameObject = function (a, b) {
    if (a == null && b == null) return true
    if (!(a && b)) return false
    if (a.val !== b.val) return false
    return isSameObject(a.left, b.left) && isSameObject(a.right, b.right)
}